package com.hutao.sso.util;


import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LRUMap;



public class ChacheManager {
	
	private static LRUMap verifyCodes = new LRUMap(100000);
	
	public static LRUMap getVerifyCodes() {
		return verifyCodes;
	}

	
	static long maxTime = 1000 * 60 * 60 * 24;
	
	public static void setCode(String id, List<Map<String, Object>> newCodeMap){
		verifyCodes.remove(id); 
		CodeSurvive cs = new CodeSurvive();
		cs.codeMap = newCodeMap;
		cs.sendTimes = new Date();
		verifyCodes.put(id, cs);
    }
	
    
	public static boolean checkCode(String id, List<Map<String, Object>> newCodeMap) {
		if (!verifyCodes.containsKey(id)) {
			return false;
		}
		CodeSurvive cs = (CodeSurvive)verifyCodes.get(id);
		if(cs == null){
			return false;
		}
		Date nowTime = Calendar.getInstance().getTime();
		
		if(nowTime.getTime() - cs.sendTimes.getTime() > maxTime){
			verifyCodes.remove(id);
			return false;
		}
		if (cs.codeMap.equals(newCodeMap)) {
			//verifyCodes.remove(id);
			return true;
		};
		
		return false;
	}
	
	
	public static List<Map<String, Object>> getCode(String id){
		CodeSurvive cs = (CodeSurvive)verifyCodes.get(id);
		if(cs == null){
			System.out.println("--------缓存已经过期--------");
			return null;
		}
		return cs.codeMap;
	}
	

}

class CodeSurvive{
	Date sendTimes;
	List<Map<String, Object>> codeMap;
}



	